package com.leonardo.beanme.fields.impl;

import java.util.Date;

import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Item;

import com.leonardo.beanme.fields.InputField;

public class DateInput implements InputField {

	private DateField field;
	private boolean lock;
		
	public DateInput() {
		this.field = new DateField("Date", DateField.DATE);
	}
	
	public void setLabel(String label) {
		field.setLabel(label);
	}
	
	public void clear() {
		field.setDate(null);
	}

	public Item getField() {
		return field;
	}

	public String getLabel() {
		return field.getLabel();
	}

	public Object getValue() {
		return field.getDate();
	}

	public void setValue(Object obj) {
		if(lock)	return;
		if(obj == null) {
			field.setDate((Date) getDefaultValue());
			return ;
		}
		try {
			field.setDate((Date) obj);
		} catch(ClassCastException ex) {
			throw new IllegalArgumentException(ex.getMessage());
		}
	}
	
	public void setName(String name) {}
	
	public void lock() {
		lock = true;
	}
	
	public void unlock() {
		lock = false;
	}
	
	public Object getDefaultValue() {
		return new Date();
	}
	
}
